﻿using System;
using System.Collections.Generic;
using System.Linq;
using BL;
using BL.Model.Request;
using DAL.Model;
using Nicekit.Excel;
using UI.Base;
using Common;

namespace UI.Reports
{
    public partial class frmInventoryItemReport : DialogViewBase 
    {
        #region Contructors

        public frmInventoryItemReport()
        {
            InitializeComponent();
        }

        #endregion

        #region Private variables

        private MasterSession _session;
        private ReportController _reportController;
        private CustomerController _customerController;
        private ProductController _productController;
        IEnumerable<GetInventoryDetail> _detailInventoryItems;

        #endregion

        protected override void InitializeBO()
        {
            base.InitializeBO();

            _session = new MasterSession();
            _reportController = new ReportController(_session);
            _customerController = new CustomerController(_session);
            _productController = new ProductController(_session);
        }

        protected override void PopulateStaticData()
        {
            base.PopulateStaticData();
            LoadCustomer();
            LoadProductCat();
            daFromDate.DateTime = CommonHelper.FirstDayOfMonth(DateTime.Now);
            daToDate.DateTime = CommonHelper.LastDayOfMonth(DateTime.Now);
        }

        protected override void PopulateData()
        {
            base.PopulateData();

            var customer = cboCustomers.EditValue != null && cboCustomers.EditValue.ToString() != "-1"
                                  ? cboCustomers.EditValue.ToString()
                                  : null;
            var productCode = string.IsNullOrEmpty(txtProductCode.Text) 
                ? null
                :txtProductCode.Text;
            var productCat = lueProductCat.EditValue != null && lueProductCat.EditValue.ToString() != "-1"
                                  ? lueProductCat.EditValue.ToString()
                                  : null;
            _detailInventoryItems = _reportController.GetInventoryDetails(daFromDate.DateTime,
                                                                          daToDate.DateTime,
                                                                          productCode,
                                                                          productCat,
                                                                          customer);
            dgInventoryItems.DataSource = _detailInventoryItems;
            gvInventoryItems.ExpandAllGroups();
            gvInventoryItems.BestFitColumns();

            PrecisionHelper.FormatCurencyPrecision(gcStockinQuantity,
                                                   gcStockOutQuantity,
                                                   gcUnitPrice,
                                                   gcTotalPayment,
                                                   gcPackageQuantity,
                                                   gcStartQuantity,
                                                   gcEndQuantity);
        }

        private void spFind_Click(object sender, EventArgs e)
        {
            PopulateData();
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {   
            if (_detailInventoryItems == null || !_detailInventoryItems.Any())
            {
                return;
            }
            
            Excel.dataGridViewToExcel(gvInventoryItems, 1, 1, null, null);
        }

        void LoadProductCat()
        {
            var request = new SearchingRequest
            {
                Keyword = string.Empty,
                IsPaging = false
            };

            var products = _productController.SearchProductCat(request);
            products.Insert(0, new ProductCategory { Id = -1, Name = string.Empty });
            lueProductCat.Properties.DataSource = products;
            lueProductCat.Properties.DisplayMember = "Name";
            lueProductCat.Properties.ValueMember = "Id";
        }

        void LoadCustomer()
        {
            var request = new SearchingRequest
            {
                Keyword = string.Empty,
                IsPaging = false
            };

            var customers = _customerController.Search(request);
            customers.Insert(0, new Customer { Id = -1, Name = string.Empty });
            cboCustomers.Properties.DataSource = customers;
            cboCustomers.Properties.DisplayMember = "Name";
            cboCustomers.Properties.ValueMember = "Id";
        }

        private void btnPrintReport_Click(object sender, EventArgs e)
        {
            var reportParams = new Dictionary<string, object>
                                   {
                                       {
                                           "@startDate", daFromDate.DateTime.ToString("yyyy-MM-dd")
                                       },
                                       {
                                           "@endDate", daToDate.DateTime.ToString("yyyy-MM-dd")
                                       },
                                       {"@productCode", txtProductCode.Text},
                                       {"@productCat", lueProductCat.EditValue},
                                       {"@CustomerId", cboCustomers.EditValue}
                                   };

            var reportViewer = new FrmReportViewer(Constant.ReportFolder + "xrptInventoryItem",
                                                   reportParams)
            {
                MdiParent = MdiParent
            };
            reportViewer.Show();
        }
    }
}
